//package com.wulian.user.task;
//
//import com.alibaba.fastjson.JSONObject;
//import com.wulian.common.constants.UserConstant;
//import com.wulian.common.dto.DtuPeopleBingInfoDto;
//import com.wulian.common.dto.UserInfo;
//import com.wulian.common.util.DateUtil;
//import com.wulian.common.vo.ResultMoudel;
//import com.wulian.data.entity.IndexDataVo;
//import com.wulian.data.enumPo.ResultDataTypeEnum;
//import com.wulian.user.entity.LogOutWatchWarning;
//import com.wulian.user.feign.DataService;
//import com.wulian.user.mapper.LogOutWatchWarningMapper;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
///*
// *@Description:(登出)
// * @author  caifulin
// * @date  2020/6/9 14:22
// * @version 1.0
// */
//
//@Component
//@Slf4j
//@Configuration
//public class LogOut {
//    @Autowired
//    private DataService dataService;
//
//    @Autowired
//    private StringRedisTemplate redisTemplate;
//
//    @Autowired
//    private  LogOutWatchWarningMapper logOutWatchWarningMapper;
//
//    //凌晨12:01的时候查询前一天夜里11:30到12:00如果该手表有在推送数据就保留token如果没有推送数据则删除token
//    @Scheduled(cron="0 5 0 * * ?")
//    //@Scheduled(cron="*/20 * * * * ?")
//    public void cardLogOut(){
//        Date date =  new Date ();
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        calendar.add(Calendar.DAY_OF_WEEK,-1);
//        calendar.set(Calendar.HOUR_OF_DAY,23);
//        calendar.set(Calendar.MINUTE,30);
//        calendar.set(Calendar.SECOND,0);
//        Date startDate = calendar.getTime();
//        calendar.set(Calendar.HOUR_OF_DAY,24);
//        calendar.set(Calendar.MINUTE,00);
//        Date endDate = calendar.getTime();
//        /*log.info("《----------------------------开始时间用假的-------------------------------》");
//        Map<String, Date> date1 = DateUtil.getDate("3");
//        startDate = date1.get("start");
//
//        System.out.println(DateUtil.DateFormat(endDate));
//        System.out.println(DateUtil.DateFormat(startDate));*/
//
//        Map<String,Object> param = new HashMap<>();
//        param.put("dateType", ResultDataTypeEnum.NORMAL.getIndex());//不聚合
//        param.put("startTime",DateUtil.DateFormat(startDate));
//        param.put("endTime",DateUtil.DateFormat(endDate));
//        param.put("resultDataType", ResultDataTypeEnum.NORMAL.getIndex());//不聚合
//        param.put("device", "bohai_sn");
//        ResultMoudel<List<IndexDataVo>> listResultMoudel = dataService.queryIndexDataByParam(param);
//        String resultFlag = listResultMoudel.getResultFlag();
//        if(resultFlag.equals("success")){
//            List<IndexDataVo> body = listResultMoudel.getBody();
//            Map<String, List<IndexDataVo>> collect = body.stream().collect(Collectors.groupingBy(IndexDataVo::getDevice));
//            Set<String> loginDevice = collect.keySet();//实时数据库里查到的sn
//            Set<String> redistDutUser = redisTemplate.keys("*_dut");
//            Set<String> redistExistSn = new HashSet<>();
//            for(String redistDut: redistDutUser){
//                String token = redisTemplate.opsForValue().get(redistDut);
//                String redisUser = redisTemplate.opsForValue().get(UserConstant.USER_TOKEN_PREFIEX+token);
//                if(StringUtils.isNotEmpty(redisUser)){
//                    UserInfo redisUserInfo = JSONObject.parseObject(redisUser, UserInfo.class);
//                    DtuPeopleBingInfoDto dtuPeopleBingInfoDto = redisUserInfo.getDtuPeopleBingInfoDto();
//                    String deviceSn = dtuPeopleBingInfoDto.getDeviceSn();
//                    redistExistSn.add(deviceSn);//获取redis上存在的sn
//                    List<String> sn = loginDevice.stream().filter(a -> a.equals(deviceSn)).collect(Collectors.toList());
//                    if(null == sn || sn.size() == 0){
//                        redisTemplate.delete("projectMap" + redisUserInfo.getDtuPeopleBingInfoDto().getId());//删除围栏信息
//                        redisTemplate.delete(redistDut);
//                        redisTemplate.delete(UserConstant.USER_TOKEN_PREFIEX+token);
//                        LogOutWatchWarning logOutWatchWarning = new LogOutWatchWarning();
//                        logOutWatchWarning.setCreateDate(date);
//                        logOutWatchWarning.setSn(deviceSn);
//                        logOutWatchWarning.setUserId(dtuPeopleBingInfoDto.getId());
//                        logOutWatchWarning.setProjectId(dtuPeopleBingInfoDto.getItemId());
//                        logOutWatchWarning.setDelFlag(0);
//                        logOutWatchWarningMapper.insert(logOutWatchWarning);
//                    }
//                }
//            }
//        }
//    }
//}
